/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Dec 8, 2006
 * 
 */

package dasregistry;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.biojava.services.das.dao.DasSourceManager;
import org.biojava.services.das.registry.DasRegistrySql;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TrafficLightBean {

	public TrafficLightBean(){
		
	}
	
	
	public Map getServerStatus(String dasSourceUrl){
		
		DataSource dataSource;
		
		Map traffic = new HashMap();
		
		try {
			Context env = (Context) new InitialContext().lookup("java:comp/env");
			 dataSource = (DataSource) env.lookup(DasRegistrySql.DATASOURCE_NAME);
		} catch (Exception e) {
			e.printStackTrace();
			return traffic;
		}   
		
		BeanFactory ctx=(new ClassPathXmlApplicationContext("springDAO.xml"));
		DasSourceManager dao=(DasSourceManager) ctx.getBean("dasSourceManager");
		
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			int auto_id = dao.getAutoIdForURL(dasSourceUrl);			
			traffic = dao.getTrafficLightData(conn, auto_id);
			
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (conn != null) {
				try { 
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return traffic;
	}
	
}
